package com.aos.erp.schools.service.branchschool.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.constant.GlobalConstant;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.mapper.branchschool.BranchSchoolMapper;
import com.aos.erp.schools.mapper.registerschool.RegisterSchoolMapper;
import com.aos.erp.schools.model.branchschool.entity.BranchSchool;
import com.aos.erp.schools.model.branchschool.ro.BranchSchoolRo;
import com.aos.erp.schools.model.branchschool.ro.UpdateBranchSchoolRo;
import com.aos.erp.schools.model.branchschool.vo.BranchSchoolVo;
import com.aos.erp.schools.model.branchschool.vo.ListBranchSchoolVo;
import com.aos.erp.schools.model.registerschool.entity.RegisterSchool;
import com.aos.erp.schools.model.registerschool.ro.RegisterSchoolRo;
import com.aos.erp.schools.model.registerschool.ro.UpdateRegisterSchoolRo;
import com.aos.erp.schools.model.registerschool.vo.ListRegisterSchoolVo;
import com.aos.erp.schools.model.registerschool.vo.RegisterSchoolVo;
import com.aos.erp.schools.service.branchschool.BranchSchoolService;
import com.aos.erp.schools.service.sys.DepartmentService;
import com.aos.erp.schools.util.UserUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 报名点 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2018-10-31
 */
@Service
public class BranchSchoolServiceImpl extends ServiceImpl<BranchSchoolMapper, BranchSchool> implements BranchSchoolService {

	@Resource
	private BranchSchoolMapper branchSchoolMapper;

	@Resource
	private DepartmentService departmentService;

	@Resource
	GlobalConstant globalConstant;


	@Override
	public int addBranch(BranchSchoolRo branchSchoolRo) throws Exception {
		//根据上级驾校Id获取省市，和车管所id
		BranchSchool parentSchool = baseMapper.selectById(branchSchoolRo.getParentId());

		/////////////////////////////////
		//获取depCode
		String depCode=departmentService.getDepCodeBranch(parentSchool.getDepCode(),"F");

		//转换ro
		BranchSchool BranchSchool=new BranchSchool();
		BeanUtils.copyProperties(branchSchoolRo,BranchSchool);
		Date date=new Date();
		BranchSchool.setDivisionId(parentSchool.getDivisionId());
		BranchSchool.setProvinceId(parentSchool.getProvinceId());
		BranchSchool.setCityId(parentSchool.getCityId());
		BranchSchool.setAreaId(parentSchool.getAreaId());
		BranchSchool.setType(3);//为分校
		BranchSchool.setRemark("系统添加分校");
		BranchSchool.setDepCode(depCode);
		BranchSchool.setCreated(date);
		BranchSchool.setUpdated(date);
		BranchSchool.setIsDel(0);//默认不删除
		BranchSchool.setStatus(0);
		BranchSchool.setCreateUser(Long.parseLong(branchSchoolRo.getCurrentUserId()));
		int i=baseMapper.insert(BranchSchool);
		if(i==1){
			//新增组织
			departmentService.addBeanSchoolBranch(parentSchool.getDepCode(),depCode,branchSchoolRo.getName(),"F",globalConstant.getAPP_CODE_SCHOOL());
		}
		return i;
	}



	@Override
	public BranchSchoolVo branchInfo(Long id) {
		BranchSchool branchSchool=baseMapper.selectById(id);
		BranchSchool parentSchool=baseMapper.selectById(branchSchool.getParentId());
		BranchSchoolVo branchSchoolVo=new BranchSchoolVo();
		branchSchoolVo.setGatherSchoolId(parentSchool.getId());
		BeanUtils.copyProperties(branchSchool,branchSchoolVo);
		return branchSchoolVo;
	}

	@Override
	public ResultEntity updateBranch(UpdateBranchSchoolRo updateBranchSchoolRo) {
		//根据上级驾校Id获取省市，和车管所id
		BranchSchool parentSchool = baseMapper.selectById(updateBranchSchoolRo.getParentId());
		BranchSchool branchSchool=new BranchSchool();
		BeanUtils.copyProperties(updateBranchSchoolRo,branchSchool);
		branchSchool.setDivisionId(parentSchool.getDivisionId());
		branchSchool.setProvinceId(parentSchool.getProvinceId());
		branchSchool.setCityId(parentSchool.getCityId());
		branchSchool.setAreaId(parentSchool.getAreaId());
		branchSchool.setUpdated(new Date());
		/*branchSchool.setCreateUser(updateBranchSchoolRo.getCurrentUserId());*/
		int count = branchSchoolMapper.updateBranch(branchSchool);
		return ResultEntity.success();
	}


	@Override
	public boolean deleteBranch(Long id) throws BizException {
		BranchSchool branchSchool=this.getById(id);
		if(branchSchool==null){
			throw new BizException("分校信息错误");
		}
		if(branchSchool.getType()!=3){
			throw new BizException("请选择分校ID");
		}
		branchSchool.setIsDel(-1);//设置为逻辑删除
		return this.saveOrUpdate(branchSchool);
	}

	@Override
	public IPageBean queryByPage(Integer pageIndex, Integer pageSize, String name,String currentUserId) throws Exception {
		IPage<ListBranchSchoolVo> page = new Page<>(pageIndex==null?1:pageIndex,pageSize==null?10:pageSize);
		String schoolId=UserUtil.getUserSchoolId(currentUserId);
		BranchSchool branchSchool=this.baseMapper.selectById(schoolId);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("schoolId",schoolId);
		map.put("depCode",branchSchool.getDepCode());
		map.put("name",name);
		map.put("type",3);//分校类型
		map.put("IPage",page);
		List<ListBranchSchoolVo> list=branchSchoolMapper.queryList(map);
		page.setRecords(list);
		return baseMapper.toBizPage(page);

	}

	/**
	 * 根据总校ID获取分校
	 * @[param id  总校ID
	 * @return
	 */
	@Override
	public List<ListBranchSchoolVo> getInfoByGather(Long id) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("parentId",id);
		map.put("type",3);//分校类型
		List<ListBranchSchoolVo> list=branchSchoolMapper.queryList(map);
		return list;
	}
}
